/**
 * Created by dqw on 2015/12/30.
 */

//列表开始
var dtGridColumns = [
    {
        id: 'id',
        title: 'ID',
        type: 'number',
        columnClass: 'text-center width-20',
        fastSort: true,
        fastQuery: true,
        fastQueryType: 'eq',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if (record.status != 8) {
                return '<a href="' + ncGlobal.adminRoot + 'orderHold/look?id=' + record.id + '">' + value + '</a>';
            }else {
                return value;
            }
        }
    },
    {
        id: 'userId',
        title: '客户ID',
        type: 'number',
        headerClass: 'text-left',
        columnClass: 'text-left width-20',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if (record.status != 8) {
                return '<a href="' + ncGlobal.adminRoot + 'user/look?id=' + record.userId + '">' + value + '</a>';
            }else {
                return value;
            }
        }
    },
    {
        id: 'inviterId',
        title: '上级ID',
        type: 'number',
        headerClass: 'text-left',
        columnClass: 'text-left width-20',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            return '<a href="' + ncGlobal.adminRoot + 'user/look?id=' + record.inviterId + '">' + value + '</a>';
        }
    },
    {
        id: 'u.userName',
        title: '客户姓名',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left width-20',
        fastSort: true,
        fastQuery: true,
        fastQueryType: 'lk',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if (record.status != 8) {
                return '<a href="' + ncGlobal.adminRoot + 'orderHold/look?id=' + record.id + '">' + value + '</a>';
            }else {
                return value;
            }
        }
    },
    // {
    //     id: 'phone',
    //     title: '电话',
    //     type: 'string',
    //     headerClass: 'text-left',
    //     columnClass: 'text-left',
    //     fastSort: false
    // },
    {
        id: 'equityCode',
        title: '股票代码',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },

    {
        id: 'equityName',
        title: '股票名称',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'tradeCredit',
        title: '点买额度(万)',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        fastSort: false,
        fastQuery: true,
        // format: '#,##0',
        fastQueryType: 'range'
    },
    // {
    //     id: 'buyPrice',
    //     title: '点买价格',
    //     type: 'number',
    //     headerClass: 'text-center',
    //     columnClass: 'text-right',
    //     fastSort: false,
    //     fastQuery: true,
    //     format: '#,##0.00',
    //     fastQueryType: 'range'
    // },
    {
        id: 'buyNumber',
        title: '点买数量',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        // format: '#,##0.00',
        fastSort: false
    },
    {
        id: 'buyDealPrice',
        title: '买入成交价格',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        format: '#,##0.000',
        fastSort: false
    },
    {
        id: 'dealNumber',
        title: '买入成交数量',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        format: '#,##0.00',
        fastSort: false
    },
    {
        id: 'i.userName',
        title: '投资人姓名',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    // {
    //     id: 'sellPrice',
    //     title: '卖出价格',
    //     type: 'number',
    //     headerClass: 'text-center',
    //     columnClass: 'text-right',
    //     format: '#,##0.00',
    //     fastSort: false
    // },
    // {
    //     id: 'sellNumber',
    //     title: '卖出数量',
    //     type: 'number',
    //     headerClass: 'text-center',
    //     columnClass: 'text-right',
    //     fastSort: false,
    //     fastQuery: true,
    //     format: '#,##0',
    //     fastQueryType: 'range'
    // },
    {
        id: 'sellDealPrice',
        title: '卖出成交价格',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        format: '#,##0.000',
        fastSort: false
    },
    {
        id: 'sellDealNumber',
        title: '卖出成交数量',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        format: '#,##0',
        fastSort: false
    },
    // {
    //     id: 'buyTime',
    //     title: '点买时间',
    //     type: 'date',
    //     format: 'yyyy-MM-dd',
    //     headerClass: 'text-left',
    //     columnClass: 'text-left',
    //     fastSort: false,
    //     fastQuery: true,
    //     fastQueryType: 'range'
    // },
    {
        id: 'profitStopPrice',
        title: '止盈价格',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        fastSort: true,
        fastQuery: false,
        format: '#,##0.00'
    },
    {
        id: 'loseStopPrice',
        title: '止损价格',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        fastSort: true,
        fastQuery: false,
        format: '#,##0.00'
    },
    {
        id: 'buyDealTime',
        title: '点买成交时间',
        type: 'date',
        format: 'yyyy-MM-dd',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range'
    },
    {
        id: 'acceptTime',
        title: '投资人接单时间',
        type: 'date',
        format: 'yyyy-MM-dd',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range'
    },
    {
        id: 'eveningUpTime',
        title: '发起平仓时间',
        type: 'date',
        format: 'yyyy-MM-dd',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range'
    },
    // {
    //     id: 'sellTime',
    //     title: '卖出时间',
    //     type: 'date',
    //     format: 'yyyy-MM-dd',
    //     headerClass: 'text-left',
    //     columnClass: 'text-left',
    //     fastSort: false,
    //     fastQuery: true,
    //     fastQueryType: 'range'
    // },
    {
        id: 'sellDealTime',
        title: '卖出成交时间',
        type: 'date',
        format: 'yyyy-MM-dd',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range'
    },
    {
        id: 'settleTime',
        title: '审核时间',
        type: 'date',
        format: 'yyyy-MM-dd',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range'
    },
    {
        id: 'profitCount',
        title: '总盈亏',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        fastSort: false,
        // format: '#,##0.000'
    },
    {
        id: 'status',
        title: '订单状态',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center width-120',
        codeTable:{
            1:"匹配中",
            2:"交易中",
            3:"持仓中",
            4:"待平仓",
            5:"平仓中",
            6:"待结算",
            7:"已结算",
            8:"流单"
        },
        fastSort: false,
        fastQuery: true,
        fastQueryType: "eq",
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if (record.status == 1) {
                return '<span style="background:#008000;padding:2px 5px;color:white;">匹配中</span>';
            }
            if (record.status == 2) {
                return '<span style="background:#568058;padding:2px 5px;color:white;">交易中</span>';
            }
            if (record.status == 3) {
                return '<span style="background:#0000FF;padding:2px 5px;color:white;">持仓中</span>';
            }
            if (record.status == 4) {
                return '<span style="background:#FFDF9B;padding:2px 5px;color:white;">待平仓</span>';
            }
            if (record.status == 5) {
                return '<span style="background:#008000;padding:2px 5px;color:white;">平仓中</span>';
            }
            if (record.status == 6) {
                return '<span style="background:#FFDF9B;padding:2px 5px;color:white;">待结算</span>';
            }
            if (record.status == 7) {
                return '<span style="background:black;padding:2px 5px;color:white;">已结算</span>';
            }
            if (record.status == 8) {
                return '<span style="background:#747372;padding:2px 5px;color:white;">流单</span>';
            }
        }
    },
    {
        id: 'opt',
        title: '操作',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: true,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var html = "";
            // 判断是否有编辑权限
            if (record.status == 2) {
                html = "<a class='btn btn-xs btn-primary m-r-10' onclick='_backFill(this)' data-no='" + dataNo + "'><i class='fa fa-file-archive-o'></i> 回填</a>";
            } else if(record.status == 4) {
                html = "<a data-target='#closePositionModal' class='btn btn-xs btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "'><i class='fa fa-close'></i> 平仓</a>";
            } else if(record.status == 5) {
                html = "<a data-target='#sellModal' class='btn btn-xs btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "'> <i class='fa fa-sellsy'></i> 点卖</a>";
            }
            // else if(record.status == 6) {
            //     html = "<a data-target='#verifyModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "'><i class='fa fa-eject'></i> 审核</a>";
            // }
            return html;
        }
    }
];

var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + 'order/list.json',
    exportURL:ncGlobal.adminRoot + 'order/listExport.json',
    exportFileName: '方案列表',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {
        int: ["id", "userId", "limit", "investId", "tradeCredit", "buyDealNumber","inviterId"],
        Timestamp: ["buyTime", "acceptTime", "buyDealTime", "eveningUpTime", "sellTime", "sellDealTime", "settleTime"],
        BigDecimal: ["buyDealPrice", "buyPrice", "sellPrice", "profitCount","profitStopPrice","loseStopPrice"]
    },
    tools:'refresh|faseQuery|export[excel]',
};

var grid = $.fn.DtGrid.init(dtGridOption);
//默认查询条件
grid.fastQueryParameters = {};
if ($('#roleId').val()== 3){
    //销售
    grid.fastQueryParameters['eq_inviter_id'] = $('#userId').val();
}
if ($('#roleId').val()== 2){
    grid.fastQueryParameters['eq_investor_id'] = $('#userId').val();
}
if ($('#roleId').val()== 0){
}
//排序
grid.sortParameter.columnId = 'id';
grid.sortParameter.sortType = 1;
//列表结束

//操作处理开始
var OperateHandle = function () {

    function _bindEvent() {

        //平仓按钮点击事件
        $("#closePositionModal").on("show.bs.modal", function (event) {
            //获取接受事件的元素
            var button = $(event.relatedTarget);
            //获取data 参数
            var datano = button.data('no');
            var modal = $(this);
            //获取列表框中的原始数据
            var gridData = grid.sortOriginalDatas[datano];
            modal.find(".oId").val(gridData.id);
            //清除错误信息
            $(".alert-danger").remove();
            $("#closePositionForm").psly().reset();
        });

        //点卖按钮点击事件
        $("#sellModal").on("show.bs.modal", function (event) {
            //获取接受事件的元素
            var button = $(event.relatedTarget);
            //获取data 参数
            var datano = button.data('no');
            var modal = $(this);
            //获取列表框中的原始数据
            var gridData = grid.sortOriginalDatas[datano];

            $("#equityCode_sell").val(gridData.equityCode);
            $("#equityName_sell").val(gridData.equityName);
            $("#buyDealNum").val(gridData.buyDealNumber);
            $("#buyDealPrice_sell").val(gridData.buyDealPrice);
            modal.find(".oId").val(gridData.id);
        });

        //审核按钮点击事件
        $("#verifyModal").on("show.bs.modal", function (event) {
            //清除错误信息
            $(".alert-danger").remove();
            $("#verifyForm").psly().reset();

            //获取接受事件的元素
            var button = $(event.relatedTarget);
            //获取data 参数
            var datano = button.data('no');
            var modal = $(this);
            //获取列表框中的原始数据
            var gridData = grid.sortOriginalDatas[datano];
            $("#equityCode_verify").val(gridData.equityCode);
            $("#equityName_verify").val(gridData.equityName);
            $("#buyDealNum_verify").val(gridData.buyDealNumber);
            $("#buyDealPrice_verify").val(gridData.buyDealPrice);
            $("#sellDealPrice_verify").val(gridData.sellDealPrice);
            $("#sellDealNumber_verify").val(gridData.sellDealNumber);
            modal.find(".oId").val(gridData.id);
        });

        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters['lk_userId'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });
    }

    //外部可调用
    return {
        bindEvent: _bindEvent
    }
}();

//操作处理结束

/**
 * 回填数据
 * @param event
 * @private
 */
function _backFill(event) {
    var datano = $(event).attr("data-no");
    //获取列表框中的原始数据
    var gridData = grid.sortOriginalDatas[datano];
    $("#equityName").val(gridData.equityName);
    $("#equityCode").val(gridData.equityCode);
    $("#buyNumber").val(gridData.buyNumber);
    $("#buyPrice").val(gridData.buyPrice);
    $("#buyDealNumber").val('');
    $(".orderId").val(gridData.id);
    $("#buyDealPrice").val('');
    $("#backFillModal").modal("show");
}

$(function () {
    //加载列表
    grid.load();
    //页面绑定事件
    OperateHandle.bindEvent();
});